package com.vitabuybuy.tv.ui.mine.talent.usercenter

import android.os.Bundle
import androidx.core.os.bundleOf
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.toast.XToast
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityLiveIncomeWithdrawBinding
import com.vitabuybuy.tv.ui.UniversalityResultActivity
import com.vitabuybuy.tv.ui.mine.talent.usercenter.viewmodel.LiveIncomeManageViewModel
import kotlinx.android.synthetic.main.activity_live_income_withdraw.*

class LiveIncomeWithdrawActivity :
    BaseActivity<LiveIncomeManageViewModel, ActivityLiveIncomeWithdrawBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_live_income_withdraw

    private var goldQuality :Int?= 0

    override fun onGetBundle(bundle: Bundle?) {
        goldQuality = bundle?.getInt("goldQuality")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.exchange_currency))
        mBinding.mViewModel = viewModel
        goldQuality?.run{
            tvConvertibleGoldCoin.text =
                String.format(getString(R.string.convertible_gold_coin), this)
            tvExchangeRatio.text = String.format(getString(R.string.exchange_ratio), 1, 100)
            tvConvertibleRmb.text = String.format(getString(R.string.convertible_rmb), (this/100))
        }
    }

    override fun eventHandling() {
        tvExchangeAll.setOnClickListener {
            goldQuality?.run {
                if (this < 100) {
                    return@setOnClickListener
                }
                val quantity = (this / 100) * 100
                etGoldCoinAmount.setText(quantity.toString())
            }
        }
        btnWithDraw.setOnClickListener {
            val quantity = etGoldCoinAmount.text.toString().toInt()
            if (quantity < 100) {
                XToast.normal(getString(R.string.jinbibuzhuyitiaixna))
                return@setOnClickListener
            }
            val drawQuantity = (quantity / 100) * 100
            viewModel.withDrawQuantity(drawQuantity)
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when(msg.code){
            1->{
                UniversalityResultActivity.openActivity(mContext,UniversalityResultActivity.FROM_CONVERSION_SUCCESS)
                finish()
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>, goldQuality: Int) {
            mContext.startActivity(
                bundleOf(Pair("goldQuality", goldQuality)),
                LiveIncomeWithdrawActivity::class.java
            )
        }
    }
}
